package work.juzheng;

/**
 * @author WDH
 * @description
 * @date 2025年01月06日 21:35
 */

public class Problem59 {

    public int[][] generateMatrix(int n) {

        int[][] ints = new int[n][n];
        int startX = 0;
        int startY = 0;
        //记录当前圈数
        int loop = 1;
        int count = 1;
        int i, j;

        while (loop <= n / 2) {
            for (j = startY; j < n - loop; j++) {
                ints[startX][j] = count++;
            }

            for (i = startX; i < n - loop; i++) {
                ints[i][j] = count++;
            }

            for (; j > startY; j--) {
                ints[i][j] = count++;
            }
            for (; i > startX; i--) {
                ints[i][j] = count++;
            }
            startX++;
            startY++;
            loop++;
        }
        if (n % 2 == 1) {
            ints[n / 2][n / 2] = n * n;
        }

        return ints;

    }


    public int[][] generateMatrix2(int n) {
        int[][] ans = new int[n][n];
        int left = 0, top = 0, right = n - 1, bottom = n - 1;
        int loop = 0;
        int x = 0;
        while (loop < n / 2) {
            for (int i = left; i <= right; i++) {
                ans[top][i] = x++;
            }
            top--;
            for (int i = top; i <= bottom; i++) {
                ans[i][right] = x++;
            }
            right--;
            for (int i = right; right >= left; i--) {
                ans[bottom][i] = x++;
            }
            bottom--;
            for (int i = bottom; bottom >= top; i--) {
                ans[i][left] = x++;
            }
            left++;
            loop++;
        }
        if (n % 2 != 0) ans[n / 2][n / 2] = n * n;
        return ans;
    }

    public static void main(String[] args) {
        Problem59 problem59 = new Problem59();
        System.out.println(5 / 2);
        int[][] array = problem59.generateMatrix(3);
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}
